import originJSONP from 'jsonp'

export default function jsonp(url, data, option) {
  url += (url.indexOf('?') < 0 ? "?":"&") + param( data )
  
  return new Promise((resolve,reject) => {
    originJSONP(url, option, (err,data) => {
      if(!err) {
        resolve(data)
      }else {
        reject(err)
      }
    })
  })
}

function param( data ) { // 将data处理为字符串
  let urlParams = ''
  for (let k in data) {
    let value = data[k] !== undefined ? data[k] : ''
    urlParams += `&${k}=${encodeURIComponent(value)}`
  }
  return urlParams ? urlParams.substring(1) : ''
}